{% extends 'xadmin/base.html' %}
{% load staticfiles %}
{% load node_tag %}

{% block title %}
    部件管理
{% endblock %}

{% block css %}
    <link href="{% static 'xadmin/css/plugins/toastr/toastr.min.css' %}" rel="stylesheet">
    <link href="{% static 'xadmin/css/bootstrap-switch.css' %}" rel="stylesheet">
{% endblock %}

{% block main %}
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-lg-3 col-lg-offset-9">
                {% if request.user.is_superuser %}
                    <button type="button" class="btn btn-xs btn-primary" onclick="window.location.href='{% url 'eleadd' %}?siteid={{ siteid }}&partid={{ partid }}'"><i class="fa fa-plus"></i>新增元素</button>
{#                    <button type="button" class="btn btn-xs btn-danger" onclick="users_del()"><i class="glyphicon glyphicon-remove"></i>删除</button>#}
                {% else %}
                    {% for perm_type, perm_slug_list in perm_dic.items %}
                        {% if perm_type == 'ele' %}
                            {% if 'add' in perm_slug_list %}
                                <button type="button" class="btn btn-xs btn-primary" onclick="window.location.href='{% url 'eleadd' %}?siteid={{ siteid }}&partid={{ partid }}'"><i class="fa fa-plus"></i>新增元素</button>
                            {% endif %}
                            {% if 'delete' in perm_slug_list %}
{#                                <button type="button" class="btn btn-xs btn-danger" onclick="users_del()"><i class="glyphicon glyphicon-remove"></i>删除</button>#}
                            {% endif %}
                        {% endif %}
                    {% endfor %}
                {% endif %}
            </div>
        </div>
        <div class="row">
            <div class="col-lg-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-content" style="border-width:0px 0px">
                        <div class="table-responsive">
                            <table class="table table-striped  table-hover">
                                <thead style="background: rgba(237, 240, 245, 0.5);">
                                    <tr>
                                        <th>
                                            <div class="icheckbox_square-green " style="position: relative;">
                                                <input type="checkbox" class="i-checks checkall" name="input[]" style="position: absolute; opacity: 0;">
                                            </div>
                                        </th>
                                        <th>名称</th>
                                        <th>类型</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {% for ele in ele_list %}
                                        <tr>
                                            <td>
                                                <div class="icheckbox_square-green" style="position: relative;">
                                                    <input type="checkbox" class="i-checks" name="input[]" value="{{ ele.id }}" style="position: absolute; opacity: 0;">
                                                </div>
                                            </td>
                                            <td>{{ ele.name }}</td>
                                            <td>{{ ele.get_etype_display }}</td>
                                            <td>
                                                {% if request.user.is_superuser %}
                                                    <a  class="btn btn-xs btn-outline btn-primary"  href="{% url 'eleedit' %}?siteid={{ siteid }}&eleid={{ ele.id }}">编辑</a>
                                                    <a  class="btn btn-xs btn-outline btn-warning" onclick="ele_del({{ ele.id }})">删除</a>
                                                {% else %}
                                                    {% for perm_type, perm_slug_list in perm_dic.items %}
                                                        {% if perm_type == 'ele' %}
                                                            {% if 'update' in perm_slug_list %}
                                                                <a  class="btn btn-xs btn-outline btn-primary"  href="{% url 'eleedit' %}?siteid={{ siteid }}&partid={{ ele.id }}">编辑</a>
                                                            {% endif %}
                                                            {% if 'delete' in perm_slug_list %}
                                                                <a  class="btn btn-xs btn-outline btn-warning" onclick="ele_del({{ ele.id }})">删除</a>
                                                            {% endif %}
                                                        {% endif %}
                                                    {% endfor %}
                                                {% endif %}

                                            </td>
                                            </td>
                                        </tr>
                                    {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div id="pagnitor" style="margin-left:40px;"></div>
        </div>
    </div>

{% endblock %}

{% block script %}
    <script src="{% static 'xadmin/js/bootstrap-switch.js' %}"></script>
    <script src="{% static 'xadmin/js/plugins/toastr/toastr.min.js' %}"></script>
    <script src="{% static 'xadmin/js/myjs/toastr_customer.js' %}"></script>
    <script>
        //复选框初始化
        $('.i-checks').iCheck({
            checkboxClass: 'icheckbox_square-green dcheckbox',
            radioClass: 'iradio_square-green',
        });
        //全选事件
        $('.checkall').on('ifChecked', function(event){
            $('.i-checks:not(:first)').iCheck('check');
        });
        $('.checkall').on('ifUnchecked', function(event){
            $('.i-checks:not(:first)').iCheck('uncheck');
        });



        function ele_del(eleid){
            layer.confirm('要删除这个元素吗?', function(index){
                layer.close(index);
                $.ajax({
                    url: '{% url 'eledel' %}',
                    type: 'post',
                    dataType: 'json',
                    data: {'eleid': eleid, 'csrfmiddlewaretoken': '{{ csrf_token }}'},
                    success: function(bk){
                        if(bk.status == '200'){
                            send_success_message(bk.message);
                        }else{
                            toastr.error(bk.message);
                        }
                    }
                })
            })
        }
        //批量删除
        function articles_del(){
            var id_list = [];
            $('.i-checks:not(:first)').each(function(){
                if($(this).get(0).checked){
                    id_list.push($(this).val());
                }
            });
            if(id_list.length > 0){
                layer.confirm('将删除选中的文章, 是否继续?', {icon: 3}, function(index){
                    layer.close(index);
                    $.ajax({
                        url: '{% url 'articlesdel' %}',
                        type: 'post',
                        dataType: 'json',
                        data: {'ids': id_list.join(','), 'csrfmiddlewaretoken': '{{ csrf_token }}'},
                        success:function(bk){
                            if(bk.status == '200'){
                                send_success_message(bk.message);
                            }
                            else{
                                toastr.error(bk.message);
                            }
                        }
                    });
                })
            }
            else{
                toastr.error('请选择要删除的文章');
            }
        }

        //批量移动
        function articles_move(){
            var id_list = [];
            $('.i-checks:not(:first)').each(function(){
                if($(this).get(0).checked){
                    id_list.push($(this).val());
                }
            });
            if(id_list.length > 0){
                var ids = id_list.join(',');
                layer.open({
                    type: 2,
                    content: '{% url "articlesmove" %}?siteid={{ siteid }}&ids=' + ids,
                    area: ['800px', '600px'],
                    title: '请点击要转移到的栏目, 只能选择叶子节点'
                });
            }
            else{
                toastr.error('请选择要移动的文章');
            }
        }
        //批量复制
        function articles_copy(){
            var id_list = [];
            $('.i-checks:not(:first)').each(function(){
                if($(this).get(0).checked){
                    id_list.push($(this).val());
                }
            });
            if(id_list.length > 0){
                var ids = id_list.join(',');
                layer.open({
                    type: 2,
                    content: '{% url "articlescopy" %}?siteid={{ siteid }}&ids=' + ids,
                    area: ['800px', '600px'],
                    title: '请选择要复制到哪些栏目'
                });
            }
            else{
                toastr.error('请选择要复制的文章');
            }
        }

        //添加到组
        function addGroup(articleid){
            layer.open({
                type: 2,
                content: '{% url "articleaddgroup" %}?siteid={{ siteid }}&articleid=' + articleid,
                area: ['800px', '600px'],
                title: '请选择文章组'
            })
        }

        //文章组筛选
        $('#groupid').change(function(){
            var groupid = $(this).val();
            if(groupid == '0'){
                window.location.href = '{% url "articlelist" %}?siteid=1';
            }
            else{
                window.location.href = '{% url "articlefiltergroup" %}?siteid={{ siteid }}&groupid=' + groupid;
            }
        })

        function modify_password(userid){
            userid = parseInt(userid);
            layer.open({
                type: 2,
                offset: 'auto',
                area: ['400px', '300px'],
                anim: 0,
                closeBtn: 2,
                title: '修改密码',
                content: '/admin/user/password/set/' + userid,
                shade: 0.4,
            })
        }
    </script>

{% endblock %}